package com.slicktechnologies.shared.common.businessunitlayer;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.io.Serializable;










import com.google.gwt.core.shared.GwtIncompatible;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Embed;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.OnLoad;
import com.googlecode.objectify.annotation.OnSave;
import com.simplesoftwares.client.library.helperconstants.ColumnDatatype;
import com.simplesoftwares.client.library.helperconstants.FormTypes;
import com.simplesoftwares.rebind.annatonations.SearchAnnotation;
import com.simplesoftwares.rebind.annatonations.SearchCompositeAnnatonation;
import com.simplesoftwares.rebind.annatonations.TableAnnotation;
import com.slicktechnologies.client.config.configurations.ConfigTypes;
import com.slicktechnologies.server.GenricServiceImpl;
import com.slicktechnologies.shared.MyUtility;
import com.slicktechnologies.shared.common.helperlayer.Config;
import com.slicktechnologies.shared.common.helperlayer.DocumentUpload;

/**
 * Represents Company
 */
@Entity

public class Company extends BusinessUnit implements Serializable
{
	/***********************************************Entity Attributes****************************************************/
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 7164313201588780483L;

	public static final String ACTIVE = "Active";

	public static final String DEMO = "Demo";

	public static final String EXPIRED = "Expired";

	public static final String INACTIVE = "Inacttive";

	/** The service tax no. */
	protected String serviceTaxNo;

	/** The vat tax no. */
	protected String vatTaxNo;

	/** The pan card. */
	protected String panCard;

	/** The logo. */
	protected DocumentUpload logo;

	/** The license. */
	protected String license;

	/** The company type. */
	@Index 
	protected String companyType;
	
	@Index
	protected Integer noOfUser;
	
	protected Integer noOfWeeks;
	
	@Index
	protected String accessUrl;
	@Index
	protected Date licenseStartDate;
	@Index
	protected String status;

	/***********************************************Relational Attributes****************************************************/
	Key<Config> companyTypeKey;

	/*************************************Default Ctor******************************************************/
	/**
	 * Instantiates a new company.
	 */
	
	
	public Company()
	{
		super();
		logo=new DocumentUpload();
		serviceTaxNo="";
		vatTaxNo="";
		panCard="";
		license="";
		companyType="";
		accessUrl="";
	}
	
	
	/**
	 * Gets the service tax no.
	 * @return the service tax no
	 */
	public String getServiceTaxNo() 
	{
		return serviceTaxNo;
	}

	/**
	 * Sets the service tax no.
	 * @param serviceTaxNo the new service tax no
	 */
	public void setServiceTaxNo(String serviceTaxNo)
	{
		if(serviceTaxNo!=null)
			this.serviceTaxNo = serviceTaxNo.trim();
	}

	/**
	 * Gets the vat tax no.
	 * @return the vat tax no
	 */
	public String getVatTaxNo()
	{
		return vatTaxNo;
	}

	/**
	 * Sets the vat tax no.
	 * @param vatTaxNo the new vat tax no
	 */
	public void setVatTaxNo(String vatTaxNo)
	{
		if(vatTaxNo!=null)
			this.vatTaxNo = vatTaxNo.trim();
	}
	/**
	 * Gets the pan card.
	 * @return the pan card
	 */
	public String getPanCard()
	{
		return panCard;
	}


	/**
	 * Sets the pan card.
	 * @param panCard the new pan card
	 */
	public void setPanCard(String panCard) 
	{
		if(panCard!=null)
			this.panCard = panCard.trim();
	}

	/**
	 * Gets the logo.
	 * @return the logo
	 */
	public DocumentUpload getLogo() 
	{
		return logo;
	}

	/**
	 * Sets the logo.
	 * @param logo the new logo
	 */
	public void setLogo(DocumentUpload logo) 
	{
		this.logo = logo;
	}
	
	/**
	 * Gets the license.
	 * @return the license
	 */
	public String getLicense() 
	{
		return license;
	}

	/**
	 * Sets the license.
	 * @param license the new license
	 */
	public void setLicense(String license) 
	{
		if(license!=null)
			this.license = license.trim();
	}

	/**
	 * Sets the company type.
	 * @param companyType the new company type
	 */
	public void setCompanyType(String companyType)
	{
		if(companyType!=null)
			this.companyType=companyType.trim();
	}

	/**
	 * Gets the company type.
	 * @return the company type
	 */
	public String getCompanyType()
	{
		return companyType;
	}

	//corresponding to  toString field. To String field can be only one 

	/* (non-Javadoc)
	 * @see com.simplesoftwares.shared.businessunitlayer.BusinessUnit#toString()
	 */
	@Override
	public String toString()
	{
		return this.buisnessUnitName;
	}
	
	public static List<String> getStatusList()
	{
		ArrayList<String> custstatuslist=new ArrayList<String>();
		custstatuslist.add(ACTIVE);
		custstatuslist.add(DEMO);
		custstatuslist.add(EXPIRED);
		custstatuslist.add(INACTIVE);
		return custstatuslist;
	}

	
	/**************************************************Relation Managment Part**********************************************/
	@OnSave
	@GwtIncompatible
	public void onSave()
	{
		if(id==null)
			setLicenseStartDate(GenricServiceImpl.parseDate());
		companyTypeKey=MyUtility.getConfigKeyFromCondition(companyType,ConfigTypes.COMPANYTYPE.getValue());
		
	}
	
	@OnLoad
	@GwtIncompatible
	public void OnLoad()
	{
		String companyName=MyUtility.getConfigNameFromKey(companyTypeKey);
		if(companyName!=null&&(companyName.equals("")==false))
		{
			if(companyName.equals("")==false)
				setCompanyType(companyName);
		}
	}

	@SearchAnnotation(Datatype = ColumnDatatype.INTEGERBOX, flexFormNumber = "10", title = "No. of Users", variableName = "noOfUsers")
	@TableAnnotation(ColType = FormTypes.TEXTCOLUM, colNo = 1, isFieldUpdater = false, isSortable = false, title = "License Start Date")
	public Integer getNoOfUser() {
		return noOfUser;
	}

	public void setNoOfUser(Integer noOfUser) {
		this.noOfUser = noOfUser;
	}

	public Integer getNoOfWeeks() {
		return noOfWeeks;
	}

	public void setNoOfWeeks(Integer noOfWeeks) {
		this.noOfWeeks = noOfWeeks;
	}

	@SearchAnnotation(Datatype = ColumnDatatype.DATECOMPARATOR, flexFormNumber = "00", title = "License Start Date", variableName = "dateComparator",extra="Lead")
	@TableAnnotation(ColType = FormTypes.DATETEXTCELL, colNo = 3, isFieldUpdater = false, isSortable = false, title = "License Start Date")
	public Date getLicenseStartDate() {
		return licenseStartDate;
	}

	public void setLicenseStartDate(Date licenseStartDate) {
		this.licenseStartDate = licenseStartDate;
	}

	@SearchAnnotation(Datatype = ColumnDatatype.LISTBOX, flexFormNumber = "01", title = "Status", variableName = "lbStatus",extra="getStatusArraylist()",colspan=2)
	@TableAnnotation(ColType = FormTypes.TEXTCOLUM, colNo = 8, isFieldUpdater = false, isSortable = true, title = "Status")
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}


	public String getAccessUrl() {
		return accessUrl;
	}


	public void setAccessUrl(String accessUrl) {
		this.accessUrl = accessUrl;
	}

	
	/****************************************************************************************************************/

   

   @GwtIncompatible
	public Date getLicenseEndDate() {
		int planDurationinDay=getNoOfWeeks()*7;
	       Date startDate=getLicenseStartDate();
	       SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
	       Calendar calendar = Calendar.getInstance();
	       calendar.setTime(startDate); // Now use today date.
	       calendar.add(Calendar.DATE, planDurationinDay);
	       return calendar.getTime();
	}


}
